package com.csmht.tododemo.service.impl;

import com.csmht.tododemo.constant.RedisKeyConstant;
import com.csmht.tododemo.mapper.BanMapper;
import com.csmht.tododemo.mapper.ReportMapper;
import com.csmht.tododemo.mapper.TodoMapper;
import com.csmht.tododemo.pojo.dto.BanDTO;
import com.csmht.tododemo.pojo.dto.CommentPageDTO;
import com.csmht.tododemo.pojo.dto.ReportDTO;
import com.csmht.tododemo.pojo.entity.Ban;
import com.csmht.tododemo.pojo.entity.Report;
import com.csmht.tododemo.pojo.entity.Todo;
import com.csmht.tododemo.pojo.enumeration.ReportEnum;
import com.csmht.tododemo.pojo.vo.ReportVO;
import com.csmht.tododemo.pojo.vo.ToDoVO;
import com.csmht.tododemo.until.BaseUnit;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ReportServiceImpl implements com.csmht.tododemo.service.ReportService {

    @Resource
    private ReportMapper reportMapper;

    @Resource
    private BanMapper banMapper;

    @Resource
    private TodoMapper todoMapper;



    @Override
    public void add(ReportDTO reportDTO) {

        Long userId = BaseUnit.getCurrentId();

        Long id = reportMapper.selectByTodoIdAndUserId(reportDTO.getTodoId(), userId);

        if(id != null){
            return;
        }

        Report report = Report.builder()
                .todoId(reportDTO.getTodoId())
                .message(reportDTO.getMessage())
                .status(ReportEnum.UNTREATED.getStatus())
                .build();

        reportMapper.insert(report);
    }


    @Override
    public void ban(BanDTO banDTO) {
        Ban ban = new Ban();
        BeanUtils.copyProperties(banDTO, ban);
        banMapper.ban(ban);

        List<Long> longList = todoMapper.selectTodoIdByUserId(ban.getUserId());

        todoMapper.updateVisitor(longList);

    }

    @Override
    public List<ReportVO> getReportTodo() {

        List<Report> list = reportMapper.getReportTodo();
        List<ReportVO> ans = new ArrayList<>();

        list.forEach(item->{
            ReportVO vo = new ReportVO();
            BeanUtils.copyProperties(item, vo);

            vo.setTodo(todoMapper.selectById(item.getTodoId()));
            ans.add(vo);
        });


        return ans;
    }
}
